package com.lkm.app_ui.help;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.widget.TextView;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.R;
import com.lkm.app_ui.m.fun.FunModule;
import com.lkm.app_ui.m.fun.OnKeyNumberEvent;
import com.lkm.comlib.help.ViewUIHelp;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.Observer;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.javatool.lang.RunnablePR;
import com.lkm.market2_0.m.steelyardserver.OnSteelyardKeyEvent;



public class HandleDialog {
    private Dialog mDialog;

    private int OK_Key;
    private Runnable okRun,cancelRun;
    private boolean isShowCancleBtn;



    public void show(Activity activity, CharSequence tips, int OK_Key, String okBtn, Runnable okRun, boolean isShowCancleBtn) {
        show(activity, tips, OK_Key, okBtn, okRun, isShowCancleBtn ? () -> {
        } : null);
    }


    public void show(Activity activity, CharSequence tips, int OK_Key, String okBtn, Runnable okRun, Runnable cancelRun) {

        this.OK_Key = OK_Key;
        this.okRun = okRun;
        this.cancelRun = cancelRun;
        this.isShowCancleBtn = cancelRun!=null;
        RunnablePR isLockRun = new RunnablePR<Void, Boolean>() {
            @Override
            public Boolean run(Void aVoid) {
                return true;
            }
        };
        if (mDialog == null) {
//            showAlertDialog(context, com.lkm.comlib.R.layout.dialog_common_alert_tips, tips, okBtn, null, okRun, null)
            mDialog = ViewUIHelp.showAlertDialog(activity, com.lkm.ui.R.layout.dialog_common_alert, tips, okBtn, isShowCancleBtn  ?"取消（任意按钮）":null, ()->{okRun.run();onOkRun();}, cancelRun);
            mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    mDialog = null;


                    LockKeyManager.removePage(isLockRun);
                    LockKeyManager.curPageLockChange();

                    dis();

                }
            });
            ((TextView) mDialog.findViewById(R.id.tv_tips)).setTextSize(40);
            ((TextView) mDialog.findViewById(R.id.tv_ok)).setTextSize(40);
            try {
                ((TextView) mDialog.findViewById(R.id.tv_cancle)).setTextSize(40);
            }catch (Exception e){}

            LockKeyManager.addPage(isLockRun);
            LockKeyManager.curPageLockChange();

            reOnKeyEvent();
        }

    }
    protected void onOkRun(){

    }

    public void dismiss() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    Observer<OnSteelyardKeyEvent> steelyardKeyEventObserver;

    Observer<OnKeyNumberEvent> keyNumberEventObserver;
    private void reOnKeyEvent() {


        LinkBus.registerReceiver(steelyardKeyEventObserver = new ObserverAutoNext<OnSteelyardKeyEvent>() {
            @Override
            public Class<OnSteelyardKeyEvent> getBusEventClass() {
                return OnSteelyardKeyEvent.class;
            }
            @Override
            public void onReceiver(OnSteelyardKeyEvent data) {
                if (!data.isIntercepted()) {
                    if(data.getKeyCode()==OnSteelyardKeyEvent.KEYCODE_FUN){

                    }else
                        data.setIntercepted(true);
                }
            }
        });



        LinkBus.registerReceiver(keyNumberEventObserver = new Observer<OnKeyNumberEvent>() {
            @Override
            public Class<OnKeyNumberEvent> getBusEventClass() {
                return OnKeyNumberEvent.class;
            }

            @Override
            public void onReceiver(OnKeyNumberEvent data, Runnable nextRun) {
                try {
                    if (data.isIntercepted()) return;


                    if (isShowCancleBtn) {
                        data.setIntercepted(true);
                    }

                    if (data.getNumber() == (float) OK_Key) {
                        data.setIntercepted(true);
                        if (okRun != null)
                            okRun.run();

                        isDis = true;
                        if (mDialog != null)
                            mDialog.dismiss();
                    } else if (isShowCancleBtn) {
                        data.setIntercepted(true);
                        if (cancelRun != null)
                            cancelRun.run();

                        isDis = true;

                        if (mDialog != null)
                            mDialog.dismiss();
                    }


                    if(isDis){

                        if(data.isFinally()){
                            dis();
                        }
                    }

                } finally {
                    nextRun.run();
                }
            }
        });
    }
    boolean isDis = false;
    private void dis(){
        LinkBus.unregisterReceiver(keyNumberEventObserver);
        LinkBus.unregisterReceiver(steelyardKeyEventObserver);
    }
}
